package cn.com.headfree.adjustment.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.adjustment.dao.IAdjustMentDao;
import cn.com.headfree.entity.TPersonYearPoint;
import cn.com.headfree.entity.TPointSetOver;
import cn.com.headfree.entity.TSalaryInfo;
import cn.com.headfree.entity.isc.User;

@Repository("/adjustMentDao")
public class AdjustDaoImpl extends BaseDaoImpl<TPersonYearPoint> implements IAdjustMentDao {

	@Override
	public void getAdjustMentPage(Page<TPersonYearPoint> page, TPersonYearPoint tPersonYearPoint) {
		String sql = "";

		sql = "select b.ID,b.salary_id,b.erp_no,b.user_name,b.org_id,b.dept_id,b.dept_name,b.year,b.last_year_point,b.year_point,b.trim_year_point,b.trim_point,b.trim_reason,b.use_point,b.use_reason,b.remain_point," +
				"b.salary_grade,b.creater_id,b.creater_name,b.CREATE_TIME,b.updater_id,b.updater_name,b.update_time,b.del_sign ";

		StringBuilder sqlbuilder = new StringBuilder(sql);
		ArrayList<Object> arrayList = new ArrayList<Object>();

		sqlbuilder.append(" from t_person_year_point b where 1=1 ");
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getOrgId())) {
			sqlbuilder.append(" AND b.org_id=? ");
			arrayList.add(tPersonYearPoint.getOrgId());
		}
		if (tPersonYearPoint.getYear()!=0) {
			sqlbuilder.append(" AND b.year = ? ");
			arrayList.add(tPersonYearPoint.getYear());
		}
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getDeptId())) {
			sqlbuilder.append(" AND b.dept_id=? ");
			arrayList.add(tPersonYearPoint.getDeptId());
		}
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getUserName())) {
			sqlbuilder.append(" AND b.user_name like concat('%',?,'%') ");
			arrayList.add(tPersonYearPoint.getUserName());
		}
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getErpNo())) {
			sqlbuilder.append(" AND b.erp_no =? ");
			arrayList.add(tPersonYearPoint.getErpNo());
		}

		sqlbuilder.append(" order by b.create_time desc  ");
		this.findPageBySql(TPersonYearPoint.class, sqlbuilder.toString(), page, arrayList.toArray());
	}

	@Override
	public void editPoint(String id, Double trimPoint, String trimReason,Double remainPoint,String salaryGrade) {
		String sql = "UPDATE t_person_year_point SET";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		if(trimPoint!=null){
			sqlBuilder.append(" trim_year_point= trim_year_point + #{trim_point,jdbcType=DOUBLE}, ");
			sqlBuilder.append(" trim_point=#{trim_point,jdbcType=DOUBLE}, ");
		}
		sqlBuilder.append(" trim_reason=#{trim_reason,jdbcType=VARCHAR}, ");
		sqlBuilder.append(" remain_point=#{remain_point,jdbcType=DOUBLE}, ");
		if(salaryGrade!=null){
			sqlBuilder.append(" salary_grade=#{salary_grade,jdbcType=VARCHAR} ");
		}
		sqlBuilder.append(" where ID=#{ID}");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("trim_point", trimPoint);
		params.put("trim_reason", trimReason);
		params.put("remain_point", remainPoint);
		params.put("salary_grade", salaryGrade);
		params.put("ID", id);
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public List<TPersonYearPoint> getAdjustMentList(TPersonYearPoint tPersonYearPoint, String ids) {
		String sql = "";

		sql = "select b.ID,b.salary_id,b.erp_no,b.user_name,b.org_id,b.dept_id,b.dept_name,b.year,b.last_year_point,b.year_point,b.trim_year_point,b.trim_point,b.trim_reason,b.use_point,b.use_reason,b.remain_point,b.salary_grade,b.creater_id,b.creater_name,b.CREATE_TIME,b.updater_id,b.updater_name,b.update_time,b.del_sign ";

		StringBuilder sqlbuilder = new StringBuilder(sql);
		ArrayList<Object> arrayList = new ArrayList<Object>();

		sqlbuilder.append(" from t_person_year_point b where 1=1 ");
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getOrgId())) {
			sqlbuilder.append(" AND b.org_id=? ");
			arrayList.add(tPersonYearPoint.getOrgId());
		}
		if (tPersonYearPoint.getYear()!=0) {
			sqlbuilder.append(" AND b.year = ? ");
			arrayList.add(tPersonYearPoint.getYear());
		}
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getDeptId())) {
			sqlbuilder.append(" AND b.dept_id=? ");
			arrayList.add(tPersonYearPoint.getDeptId());
		}
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getUserName())) {
			sqlbuilder.append(" AND b.user_name like concat('%',?,'%') ");
			arrayList.add(tPersonYearPoint.getUserName());
		}
		if (!StringUtil.isNullOrEmpty(tPersonYearPoint.getErpNo())) {
			sqlbuilder.append(" AND b.erp_no =? ");
			arrayList.add(tPersonYearPoint.getErpNo());
		}
		if (!StringUtil.isNullOrEmpty(ids)) {
			sqlbuilder.append(" AND ID IN(" + ids + ")");
		}
		sqlbuilder.append(" order by create_time desc ");
		return this.findAllBySQL(TPersonYearPoint.class, sqlbuilder.toString(), null, false,
				arrayList.toArray());
	}

	@Override
	public TPointSetOver preEndPoint(String baseOrgId, int year) {
		String sql = "  select ID,org_id,org_name,year,is_over  " +
				" from t_point_set_over t  " +
				" where t.org_id= ?  and t.year=? and t.is_over=1 and del_sign=0" ;
		ArrayList<Object> arrayList = new ArrayList<Object>();
		arrayList.add(baseOrgId);
		arrayList.add(year);
		return this.findUnique(TPointSetOver.class, sql, arrayList.toArray());
	}

	@Override
	public void endPoint(User user, int year) {
		String sql = "insert into t_point_set_over (id,org_id,org_name,year,is_over,creater_id,creater_name,create_time,updater_id,updater_name,update_time) values ";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{org_id,jdbcType=VARCHAR},")
				.append("#{org_name,jdbcType=VARCHAR},").append("#{year,jdbcType=INTEGER},")
				.append("#{is_over,jdbcType=INTEGER},").append("#{creater_id,jdbcType=VARCHAR},")
				.append("#{creater_name,jdbcType=VARCHAR},").append("#{create_time,jdbcType=DATE},")
				.append("#{updater_id,jdbcType=VARCHAR},").append("#{updater_name,jdbcType=VARCHAR},")
				.append("#{update_time,jdbcType=DATE})");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id",IdGen.uuid());
		params.put("org_id", user.getBaseOrgId());
		params.put("org_name", user.getBaseOrgName());
		params.put("year", year);
		params.put("is_over", 1);
		params.put("creater_id", user.getFax());
		params.put("creater_name", user.getName());
		params.put("create_time", new Timestamp(new Date().getTime()));
		params.put("updater_id", user.getFax());
		params.put("updater_name", user.getName());
		params.put("update_time", new Timestamp(new Date().getTime()));
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public void updateTPersonYearPoint(TPersonYearPoint tp) {
		String sql = "UPDATE t_person_year_point  SET";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" use_point=#{use_point,jdbcType=DOUBLE},");
		sqlBuilder.append(" remain_point=#{remain_point,jdbcType=DOUBLE}, ");
		sqlBuilder.append(" use_reason=#{use_reason,jdbcType=VARCHAR} ");
		sqlBuilder.append(" where ID=#{ID}");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("use_point", tp.getUsePoint());
		params.put("remain_point", tp.getRemainPoint());
		params.put("use_reason", tp.getUseReason());
		params.put("ID", tp.getId());
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public List<TSalaryInfo> getDaiSalayInfo(int year, String baseOrgId, String flag) {
		String sql = "";

		sql = "select b.ID ";

		StringBuilder sqlbuilder = new StringBuilder(sql);
		ArrayList<Object> arrayList = new ArrayList<Object>();

		sqlbuilder.append(" from t_salary_info b where 1=1 ");
		if (year!=0) {
			sqlbuilder.append(" AND b.year=? ");
			arrayList.add(year);
		}
		if (!StringUtil.isNullOrEmpty(baseOrgId)) {
			sqlbuilder.append(" AND b.org_id=? ");
			arrayList.add(baseOrgId);
		}
		if (!StringUtil.isNullOrEmpty(flag)) {
			sqlbuilder.append(" AND b.audit_status=? ");
			arrayList.add(flag);
		}
		sqlbuilder.append(" order by create_time desc ");
		return this.findAllBySQL(TSalaryInfo.class, sqlbuilder.toString(), null, false,
				arrayList.toArray());
	}

	@Override
	public void editPointDetail(String erpNo, int year, double remainPoint) {
		String sql = "UPDATE t_person_year_point_detail  SET";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" trim_total_point=#{trim_total_point,jdbcType=DOUBLE} ");
		sqlBuilder.append(" where erp_no=#{erp_no} ");
		sqlBuilder.append(" and year=#{year} ");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("trim_total_point", remainPoint);
		params.put("erp_no", erpNo);
		params.put("year", year);
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public void editPointDetail(String erpNo, int year, double remainPoint, String salaryGrade) {
		String sql = "UPDATE t_person_year_point_detail  SET";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" trim_total_point=#{trim_total_point,jdbcType=DOUBLE}, ");
		sqlBuilder.append(" trim_salary_grade=#{trim_salary_grade,jdbcType=VARCHAR} ");
		sqlBuilder.append(" where erp_no=#{erp_no} ");
		sqlBuilder.append(" and year=#{year} ");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("trim_total_point", remainPoint);
		params.put("trim_salary_grade", salaryGrade);
		params.put("erp_no", erpNo);
		params.put("year", year);
		this.executeSql(sqlBuilder.toString(), params);
	}

}
